package fatecpg.com.br.japassei.bd;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

/**
 * Created by MarcoAurélio on 28/11/2014.
 */
public class DisciplinaDAO{
    ArrayList<Disciplina>disciplinas = new ArrayList<Disciplina>();
    MySQLiteHelper sqLiteHelper;


    private static final String TABLE = "tb_disciplinas";
    private static final String KEY_ID = "cd_disciplina";
    private static final String NAME = "nm_disciplina";
    private static final String DESC = "ds_disciplina";
    private static final String P1 = "qt_prova_um";
    private static final String P2 = "qt_prova_dois";
    private static final String TP = "qt_trab_prat";
    private static final String [] COLUMNS = {"cd_disciplina", "nm_disciplina", " ds_disciplina", "qt_prova_um", "qt_prova_dois", "qt_trab_prat"};
    private static final String [] COLUMNS_NOTA = {"qt_prova_um", "qt_prova_dois", "qt_trab_prat"};

    public DisciplinaDAO(Context context){
        sqLiteHelper = new MySQLiteHelper(context);
    }

    public ArrayList<Disciplina>getDisciplinas(){
        this.disciplinas.clear();

        String query = "SELECT  * FROM " + TABLE;
        SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        Disciplina disciplina = null;
        if (cursor.moveToFirst()) {
            do {

                try {
                    Nota nota = new Nota(Double.parseDouble(cursor.getString(3)), Double.parseDouble(cursor.getString(4)), Double.parseDouble(cursor.getString(5)));
                    disciplina = new Disciplina(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), nota);
                    disciplinas.add(disciplina);
                }catch (NullPointerException ex){
                    disciplina = new Disciplina(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));
                    disciplinas.add(disciplina);
                }

            } while (cursor.moveToNext());
        }
        return this.disciplinas;
    }


    public Disciplina getDisciplina(int id){
        SQLiteDatabase db = sqLiteHelper.getReadableDatabase();
        Cursor cursor =
                db.query(TABLE, // a. table
                        COLUMNS, // b. column names
                        " id = ?", // c. selections
                        new String[] { String.valueOf(id) }, // d. selections args
                        null, // e. group by
                        null, // f. having
                        null, // g. order by
                        null); // h. limit


        if (cursor != null)
            cursor.moveToFirst();

        Disciplina disciplina = new Disciplina(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), Float.parseFloat(cursor.getString(3)),Float.parseFloat(cursor.getString(3)),Float.parseFloat(cursor.getString(3)));
        return disciplina;
    }


    public boolean setNota(int cdDisciplina, Nota nota){
        SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
        try {
            ContentValues values = new ContentValues();
            values.put(P1, nota.getNota_p1());
            values.put(P2, nota.getNota_p2());
            values.put(TP, nota.getNota_tp());
            db.update(TABLE, values, KEY_ID + " = ?", new String[]{String.valueOf(cdDisciplina)});
            return true;
        }catch(Exception ex){
            return false;
        }finally {
            db.close();
        }
    }

    public boolean setDisciplina(Disciplina disciplina){
        SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
        try {
            ContentValues values = new ContentValues();
            values.put(NAME, disciplina.getNmDisciplina());
            values.put(DESC, disciplina.getDsDisciplina());

            db.insert(TABLE,
                    null,
                    values);
            return true;
        }catch (Exception ex){
            return false;
        }finally {
            db.close();
        }
    }



    public boolean delDisciplina(int cdDisciplina){
        SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
        db.delete(TABLE, KEY_ID+" = ?",
                new String[] { String.valueOf(cdDisciplina) });
        db.close();
        return true;
    }

    public boolean upDisciplina(int cdDisciplina, String nmDisciplina, String dsDisciplina){
        SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
        try {
            ContentValues values = new ContentValues();
            values.put(NAME, nmDisciplina);
            values.put(DESC, dsDisciplina);

            int i = db.update(TABLE, //table
                    values, // column/value
                    KEY_ID + " = ?", // selections
                    new String[]{String.valueOf(cdDisciplina)}); //selection args
        }catch (Exception ex){
            return false;
        }finally {
            db.close();
        }
        return true;
    }
}
